<?php

	class MY_Controller extends CI_Controller
	{
		/*
		 * Initialize DATA
		 */
		var $data;
		var $contenttype; // news, shop		
		var $cat; // array of categories 		
		var $cat_ref; // array of category ref to id (news_type_id)

		// action page
		var $action_pages;
		
		/*
		 * Template section
		 */
		var $template;
		var $template_path;
		var $js_path;
		var $css_path;
		
		/*
		 * Environment
		 * Development, Test, Production
		 */
		var $environment;
		
		function __construct()
		{
			parent::__construct();				
		}			
		
		/*
		 * BIG CODE HERE
		 * Fetching content based on content type
		 * 
		 * For example:
		 * News
		 * getting news with defined category 
		 * all news
		 * hot news
		 * advetisements
		 */
		
		function getContent($page_action = '')
		{
			/////////////////////////////////////////////////////
			$this->makeCatReference();
			
			//////////////// PREPARE BODY'S PAGE ////////////////
			// get example of thebox.vn
			// homepage
			// content: hottest news, advertisements, categories of news
			//
			/////////////////////////////////////////////////////			

			/////////////// LOAD MODEL ////////////////		
			$this->load->model('Gs_new_news_model');
			///////////////////////////////////////////

			// title of page
			$this->data['title'] = $this->action_pages[$page_action]['title'];

			// get all data need shown
			$categories_shown = $this->action_pages[$page_action]['data'];
			
			foreach( $categories_shown as $key => $value )
			{
				// option
				$this->makeCatReference($value);

				// fetch data info into specified cat
				// condition
				$new_type_cond = 'gs_new_news.news_type_id = '. $this->cat_ref[$value]['id'];
				$this->data['data'][$value] = $this->Gs_new_news_model->selectJoinTable('*','gs_new_news_type','gs_new_news_type.id = gs_new_news.news_type_id',$new_type_cond);
			}

			/////////////////////////////////////////////////////
			switch ( $this->contenttype )
			{
				case 'news':					
					foreach( $this->cat as $each )
					{
						$new_type_cond = 'gs_new_news.news_type_id = '.$this->cat_ref[$each]['id'];
						$this->data['cat'][$each] = $this->Gs_new_news_model->selectJoinTable('*','gs_new_news_type','gs_new_news_type.id = gs_new_news.news_type_id',$new_type_cond);
					}
					break;
				default:
					break;
			}
						
			
			$this->showView($this->data);
		} 			
		
		// Utilities function
		function makeCatReference($content_type = '')
		{
			$contenttype = $this->contenttype;

			if( !empty($content_type) )
			{
				$contenttype = $content_type;
			}

			switch ( $contenttype )
			{
				case 'news':
					$this->load->model('Gs_new_news_type_model');
					foreach( $this->cat as $each )
					{
						$option = array('type_slug'	=>	$each);
						$new_type = $this->Gs_new_news_type_model->select('','',$option);
						foreach ( $new_type as $type )
						{
							$this->cat_ref[$each]['id'] = $type->id;							
						}
					}					
					break;
					
			}
		}
		
		/*
		 * BIG CODE HERE
		 * Template function 
		 * Replace result with template 
		 */
		function showView($result = '', $template = '')
		{
			if( $template == '' )
			{
				// test env
				echo '<pre>';
				print_r($result);
				echo '</pre>';
			}
			else 
			{
				switch ( $this->environment )
				{
					case 'test':
						break;
					case 'development':
						break;
					case 'production':
						$this->load->view($this->template_path.'/'.$template,$this->data);
						break;
				}
			}					
		}
		
		/*
		 * 
		 */
				
	}